package org.trajectory.query;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;


public class QueryDeserializer {
	private String xml;
	private Document dom;
	public QueryDeserializer(String xml){
		this.xml = xml;
	}
	public Query parse() throws UnsupportedEncodingException, JDOMException, IOException{
		Query query = new Query();
		SAXBuilder parser = new SAXBuilder();
		dom = parser.build((new ByteArrayInputStream(xml.getBytes("UTF8"))));
		Element root = dom.getRootElement();
		Element datasourceE = root.getChild("Datasource");
		query.setDatasource(datasourceE.getText());
		Element shopE = root.getChild("Shop");
		query.setShop(shopE.getText());
		Element lineE = root.getChild("Line");
		query.setLine(lineE.getText().split(","));
		Element fromE = root.getChild("From");
		query.setFrom(Long.valueOf(fromE.getText()));
		Element toE = root.getChild("To");
		query.setTo(Long.valueOf(toE.getText()));
		
		return query;
		
	}
}
